<?php namespace Cms\Controller; 

use Cms\Controller;

class FieldController extends Controller{

    //GET /photo 索引
    public function index()
    {
        $data = Api('Field/getAll',array(Q('mid')));
        View::with('data',$data)->make();
    }

    //获取字段模板
    public function getAddFieldView()
    {
        $type = Q('get.type');
        echo  file_get_contents(VIEW_PATH.'/Field/View/'.$type.'/add.php');
        exit;
    }

    //索引页更改排序
    public function fieldsort()
    {
        $fid = Q('fid','','intval');
        $fieldsort = Q('fieldsort','','intval');
        if(Db::table('field')->where('fid',$fid)->update(array('fieldsort'=>$fieldsort)))
        {
            $this->ajaxReturn(0,'更改成功');
        }
    }
    //GET /photo/create 创建界面
    public function create()
    {
        $model = Db::table('model')->where('mid',$_GET['mid'])->first();
        View::with('model',$model);
        View::make();
    }

    //POST /photo 保存新增数据
    public function store()
    {
        if(Api('Field/store'))
        {
            $this->ajaxReturn(0,'添加成功');
        }
        else
        {
            $this->ajaxReturn(1,Api('Field/getError'));
        }
    }

    //GET /photo/{photo} 显示文章
    public function show($id)
    {
        echo 'show';
    }

    //GET /photo/{photo}/edit 更新界面
    public function edit($fid)
    {
        $field = Api("Field/getFieldData",array($fid));
        View::with('field',$field)->make();
    }

    //PUT /photo/{photo} 更新数据
    public function update($fid)
    {
        if(Api('Field/update'))
        {
            $this->ajaxReturn(0,'更新成功');
        }
        else
        {
            $this->ajaxReturn(1,Api('Field/getError'));
        }
    }

    //DELETE /photo/{photo} 删除
    public function destroy($fid)
    {
        if(Api('Field/destroy',array($fid)))
        {
            $this->ajaxReturn(0,'删除成功');
        }
        else
        {
            $this->ajaxReturn(1,Api('Field/getError'));
        }
    }
}